import pymysql

db = pymysql.connect(
    host='localhost',
    user='root',
    password='123456',
    port=3306,
    charset='utf8',
    database='0617test'
)
try:
    with db.cursor() as cursor:
        for i in range(10):
            name = input("请输入姓名（中英文都可）：")
            age = int(input("请输入年龄（0-120）："))
            sex = input("请输入性别（男/女）：")
            height = int(input("请输入身高（）cm："))
            sql = "Insert into persons (name,age,sex,height) values (%s,%s,%s,%s)"
            params = (name, age, sex, height)
            cursor.execute(sql, params)
            db.commit()
            print(f"第{i + 1}条信息插入完毕！")
except pymysql.Error as e:
    print(f"数据库错误：{e}")
    db.rollback()  # 发生错误时回滚事务
except ValueError as e:
    print(f"输入错误：{e}")
except Exception as e:
    print(f"未知错误：{e}")
finally:
    cursor.close()
db.close()
